﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter21.Models.EventSamples
{
    using System.Threading;

    public class Calculator
    {
        private ManualResetEventSlim manualResetEventSlim;
        private CountdownEvent countdownEvent;

        public Calculator(ManualResetEventSlim manualResetEventSlim)
        {
            this.manualResetEventSlim = manualResetEventSlim;
        }
        public Calculator(CountdownEvent countdownEvent)
        {
            this.countdownEvent = countdownEvent;
        }

        public int Result { get; private set; }

        public void Calculation(int x, int y)
        {
            Console.WriteLine("任务 {0} 开始计算", Task.CurrentId);
            Thread.Sleep(new Random().Next(3000));
            Result = x + y;
            Console.WriteLine("任务 {0} 准备就绪", Task.CurrentId);
            if (manualResetEventSlim != null)
            {
                manualResetEventSlim.Set();
            }
            if (countdownEvent != null)
            {
                countdownEvent.Signal();
            }
        }
    }
}
